package p.sunmes.les;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.utils.Align;
import com.badlogic.gdx.utils.Scaling;
import com.badlogic.gdx.utils.viewport.ScalingViewport;
import com.badlogic.gdx.utils.viewport.Viewport;

import p.sunmes.les.managers.CM;
import p.sunmes.les.managers.GM;
import p.sunmes.les.managers.RM;
import p.sunmes.les.managers.SoundM;
import p.sunmes.les.util.LU;

public abstract class ExMainGame extends ApplicationAdapter {

	protected float screenWidth = 800;
	protected float screenHeight = 480;
	protected Stage stage;
	protected Label dl;

	@Override
	public void create() {
		init();
		initManager();

		Gdx.gl.glClearColor(0, 0, 0, 1);

		Viewport view = new ScalingViewport(Scaling.fit, screenWidth,
				screenHeight);
		stage = new Stage(view);
		Gdx.input.setInputProcessor(stage);

		dl = LU.getLabelWithBorder("FPS:0123456789");

		GM.debugLabel = dl;
		GM.viewport = view;
		GM.stage = stage;

		dl.setPosition(10, 10);
		dl.setAlignment(Align.bottomLeft);

		start();
	}

	/**
	 * 配置屏幕大小 screenWidth,screenHeight;
	 * 配置RM
	 */
	protected abstract void init();

	/**
	 * stage 操作.添加内容.
	 */
	protected abstract void start();

	protected void initManager() {
		RM.instance();
		CM.instance();
		SoundM.instance();

		configFileLoad();
	}

	/**
	 * CM.addLoadFile...
	 * CM.loadConfigs..
	 */
	public abstract void configFileLoad();

	@Override
	public void resize(int width, int height) {
		stage.getViewport().update(width, height, true);
	}

	@Override
	public void render() {
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

		stage.act();

		if (dl.isVisible()) {
			stage.addActor(dl);
		}

		stage.draw();
	}

	@Override
	public void dispose() {
		childDispose();

		GM.dispose();
		RM.dispose();
	}

	public abstract void childDispose();
}
